package com.example.graduation.controller;

import com.example.graduation.entity.JobType;
import com.example.graduation.service.JobTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/job-types")
public class JobTypeController {

    @Autowired
    private JobTypeService jobTypeService;

    @GetMapping
    public ResponseEntity<List<JobType>> getAllJobTypes() {
        return ResponseEntity.ok(jobTypeService.findAll());
    }

    @GetMapping("/{id}")
    public ResponseEntity<JobType> getJobTypeById(@PathVariable Integer id) {
        JobType jobType = jobTypeService.findById(id);
        if (jobType != null) {
            return ResponseEntity.ok(jobType);
        }
        return ResponseEntity.notFound().build();
    }

    @PostMapping
    public ResponseEntity<JobType> createJobType(@RequestBody JobType jobType) {
        int result = jobTypeService.insert(jobType);
        if (result > 0) {
            return ResponseEntity.ok(jobType);
        }
        return ResponseEntity.badRequest().build();
    }

    @PutMapping("/{id}")
    public ResponseEntity<JobType> updateJobType(@PathVariable Integer id, @RequestBody JobType jobType) {
        jobType.setId(id);
        int result = jobTypeService.update(jobType);
        if (result > 0) {
            return ResponseEntity.ok(jobType);
        }
        return ResponseEntity.notFound().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteJobType(@PathVariable Integer id) {
        int result = jobTypeService.deleteById(id);
        if (result > 0) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.notFound().build();
    }
} 